#include<stdio.h>
#include<string.h>
#include<typedef.h>
#include"public.c"
#include"typedef.h"


#define   log_conf_path   "./log.conf"

void default_config_log(char *pclogpath, char *pcprocess_name, int *process_num, char *pcthread_name, int *pcthread_num, char *pcdescribe, int *loglevel, int *logdebug, int *log_file_max_size)
{
	 if(*loglevel == 0)
     {
        *loglevel = 0;
     }  
     if(*logdebug == 0)
     {
        *logdebug = 0;
     } 
     if(*process_num == 0)
     {
        *process_num = 0;
     }
	 if(*pcthread_num == 0)
     {
        *pcthread_num = 0;
     }   
     if(*log_file_max_size == 0)
     {
        *log_file_max_size = 2;
     }
     if(pclogpath[0] == '\0')
     {        
        strncpy(pclogpath, "./", 255);
     }
     if(pcprocess_name[0] == '\0')
     {
        strncpy(pcprocess_name, "cl_ini", 15);
     }
     if(pcthread_name[0] == '\0')
     {
        strncpy(pcthread_name, "main", 15);
     }
	 if(pcdescribe[0] == '\0')
     {
        strncpy(pcdescribe, "CL", 15);
     }
}

void assign_log_conf(char* acsection, char *pcFilebuftemp, char *pclinebuf, char *char *pclogpath, char *pcprocess_name, int *process_num, char *pcthread_name, int *pcthread_num, char *pcdescribe, int *loglevel, int *logdebug, int *log_file_max_size)
{

	if(strcmp(acsection, "loglevel") == 0)
	{
		 *loglevel = atoi(pclinebuf -1);
	} 

	if(strcmp(acsection, "logdebug") == 0)
	{
		 *logdebug = atoi(pclinebuf -1);
	} 

	if(strcmp(acsection, "process_num") == 0)
	{
		 *process_num = atoi(pclinebuf - 1);
	} 

	if(strcmp(acsection, "pcthread_num") == 0)
	{
		*pcthread_num = atoi(pclinebuf - 1);
	} 

	if(strcmp(acsection, "log_file_max_size") == 0)
	{
		 *log_file_max_size = atoi(pclinebuf - 1);
	} 

	if(strcmp(acsection, "logpath") == 0)
	{
		 memcpy(pclogpath, pcFilebuftemp, pclinebuf - pcFilebuftemp);
	} 

	if(strcmp(acsection, "process_name") == 0)
	{
		 memcpy(pcprocess_name, pcFilebuftemp, pclinebuf - pcFilebuftemp);
	} 
	if(strcmp(acsection, "thread_name") == 0)
	{
		 memcpy(pcthread_name, pcFilebuftemp, pclinebuf - pcFilebuftemp);
	} 
	if(strcmp(acsection, "describe") == 0)
	{
		 memcpy(pcdescribe, pcFilebuftemp, pclinebuf - pcFilebuftemp);
	} 
}
int read_log_conf(char *pcfilename, char *pclogpath, char *pcprocess_name, int *process_num, char *pcthread_name, int *pcthread_num, char *pcdescribe, int *loglevel, int *logdebug, int *log_file_max_size)
{
	if(NULL == pcfilename)
	{	
		return RET_FAIL;
	}

	FILE *fp                              = NULL;
	unsigned long  ulen 				  = 0;
	char *pcFilebuf 					  = NULL;
	char *pcFilebuftemp 				  = NULL;
	char *pclinebuf 					  = NULL;
	char acsection[CL_BUF_LEN_MINI] 	  = {0};
	fp = fopen(pcfilename, "r");
	if(NULL == fp)
	{
		return RET_FAIL;
	}

	fseek(fp, 0, SEEK_END);
	ulen = ftell(fp);
	rewind(fp);

	pcFilebuf = (char *)malloc(sizeof(char) * ulen + 1);
	if(NULL == pcFilebuf)
	{
		return RET_FAIL;
	}
	memset(pcFilebuf, 0x0, ulen + 1);
	fread(pcFilebuf, ulen, 1, fp);
	*(pcFilebuf + ulen) = '\0';

	pcFilebuftemp = pcFilebuf;
	while(*pcFilebuftemp)
	{
		
		while(*pcFilebuftemp == ' ' || *pcFilebuftemp == '\t')
		{
			pcFilebuftemp++;
		}
		
		if(*pcFilebuftemp == '#' || *pcFilebuftemp == '\n' || \
			*pcFilebuftemp == '\r')
		{
			while(*pcFilebuftemp != '\r' && *pcFilebuftemp != '\n')
			{
				pcFilebuftemp++;
			}
			pcFilebuftemp++;
			continue;
		}
		
		pclinebuf = pcFilebuftemp;
		while(*pclinebuf != ' ' && *pclinebuf != '\t' && *pclinebuf!= '=' && *pclinebuf != '\0')
		{
			pclinebuf++;
		}
		if(*pclinebuf == '\0')
		{
			pcFilebuftemp = pclinebuf;
			continue;
		}
		memset(acsection, 0x0, sizeof(acsection));
		memcpy(acsection, pcFilebuftemp, pclinebuf - pcFilebuftemp);
		
		while(*pclinebuf == ' ' || *pclinebuf == '\t' || *pclinebuf== '=')
		{
			pclinebuf++;
		}
		pcFilebuftemp  = pclinebuf;
		while(*pclinebuf != ' ' && *pclinebuf != '\t' && *pclinebuf!= '\r' && *pclinebuf!= '\n' && *pclinebuf != '\0')
		{
			pclinebuf++;
		}		
		assign_log_conf(acsection,pcFilebuftemp, pclinebuf,pclogpath, pcprocess_name, process_num, pcthread_name, pcthread_num, pcdescribe, loglevel, logdebug, log_file_max_size);
		pcFilebuftemp = pclinebuf;
	} 
    default_config_log(pclogpath, pcprocess_name, process_num, pcthread_name, pcthread_num, pcdescribe, loglevel, logdebug, log_file_max_size);
	free(pcFilebuf);
	return RET_SUCC;
}

int  parse_ini()
{
	char aclogpath[MAX_PATH_LEN]			   =  {0};
	int  loglevel                              = 0;
	int  logdebug                              = 0;
	char acprocess_name[CL_BUF_LEN_LEAST]	   =  {0};	
	int  process_num                           = 0;
	int  thread_nameindex                      = 0;
	char acthread_name[CL_BUF_LEN_LEAST]	   =  {0};
	char acdescribe[CL_BUF_LEN_LEAST]		   =  {0};
	int  log_file_max_size;

	if(RET_SUCC == check_is_have_file(log_conf_path))
	{
		read_log_conf(log_conf_path, aclogpath, acprocess_name, &process_num, acthread_name, &thread_nameindex, acdescribe, &loglevel, &logdebug, &log_file_max_size);        
	}	
	else
	{     
		default_config_log(aclogpath, acprocess_name, &process_num, acthread_name, &thread_nameindex, acdescribe, &loglevel, &logdebug, &log_file_max_size);
	}	
}
int main()
{
	parse_ini();
}

